package ve.com.ucv.changedetection.model;

//import com.google.code.morphia.Datastore;
//import com.google.code.morphia.Morphia;
//import com.google.code.morphia.query.Query;
import org.apache.log4j.Logger;
import sun.java2d.pipe.SpanShapeRenderer;
import ve.com.ucv.changedetection.objects.Constants;
import ve.com.ucv.changedetection.objects.HistoricalPageInfo;
import ve.com.ucv.changedetection.objects.TransformedWebPage;
import ve.com.ucv.changedetection.objects.TreeNode;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: josmaguirre
 * Date: 6/7/13
 * Time: 12:00 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataManipulation {
    static Logger logger = Logger.getLogger(DataManipulation.class);
    /*public boolean insertTreeNode(TreeNode tree, String url){
        //CALCULANDO LA FECHA DE SOLICITUD DEL PROCESAMIENTO
        String date = getCurrentDate();
        logger.info("OBTENIENDO LA CONEXION DE LA BASE DE DATOS " + Constants.MONGODB_NAME);
        Datastore ds = ConnectionManager.getMongoDBConnection();
        if (ds != null){
            ds.ensureIndexes(); //creates indexes from @Index annotations in your entities
            ds.ensureCaps(); //creates capped collections from @Entity
            logger.info("CREANDO EL OBJETO DE LA PAGINA " + url + " PARA REGISTRARLO EN LA BASE DE DATOS");
            TransformedWebPage webPage = new TransformedWebPage(url, date, tree);
            logger.info("GUARDANDO LA PAGINA " + url);
            ds.save(webPage);
            return true;
        }
        return false;
    }*/

    public String getCurrentDate(){
        SimpleDateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy hh:mm:aaa");
        Date currentDate = new Date();
        return dateFormat.format(currentDate);
    }

    /*public ArrayList<HistoricalPageInfo> getHistoricalPages(String pageName){
        ArrayList<HistoricalPageInfo> historicalPages = new ArrayList<HistoricalPageInfo>();
        logger.info("OBTENIENDO LA CONEXION DE LA BASE DE DATOS " + Constants.MONGODB_NAME);
        Datastore ds = ConnectionManager.getMongoDBConnection();
        logger.info("REALIZANDO LA CONSULTA PARA OBTENER EL HISTORICO DE PAGINAS");
        Query webPageQuery = ds.find(TransformedWebPage.class).field("webPageName").equal(pageName);
        //ES NECESARIO ENCONTRAR LA MANERA DE QUE LA CONSULTA SE REALICE COMO SI SE UTILIZARA UN 'LIKE' EN BD

        ArrayList<TransformedWebPage> webPagesResult = (ArrayList<TransformedWebPage>)webPageQuery.asList();
        logger.info("CONSULTA REALIZADA");
        logger.info("NUMERO DE PAGINAS ENCONTRADAS: " + webPagesResult.size());

        for(TransformedWebPage twp : webPagesResult){
            HistoricalPageInfo historicalPage = new HistoricalPageInfo();
            historicalPage.setWebPageName(twp.getWebPageName());
            historicalPage.setWebPageDate(twp.getProccesedDate());
            historicalPages.add(historicalPage);
            logger.info("PAGINA ENCONTRADA: " + historicalPage.getWebPageName() + " - " + historicalPage.getWebPageDate());
        }

        return historicalPages;
    }*/

    /*public TreeNode getAbstractTree(String url, String date){
        //TreeNode abstractTree = new TreeNode();
        logger.info("OBTENIENDO LA CONEXION DE LA BASE DE DATOS " + Constants.MONGODB_NAME);
        Datastore ds = ConnectionManager.getMongoDBConnection();
        logger.info("REALIZANDO LA CONSULTA PARA OBTENER EL ARBOL DE LA PAGINA " + url + " PARA LA FECHA " + date);
        Query treeQuery = ds.find(TransformedWebPage.class).field("webPageName").equal(url).field("proccesedDate").equal(date);
        TransformedWebPage twp = (TransformedWebPage)treeQuery.get();


        return twp.getWebPageTree();
    }*/
}
